//
//  NewsViewController.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/19.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "NewsViewController.h"
#import "LoginViewController.h"
#import "APIService+Login.h"
#import "AppDelegate.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - 点击下方按钮切换至登录页面 -
- (void)switchToTestSelfView:(UIButton *)sender
{
    //CATransition *animation = [CATransition animation];
    //animation.duration = 1.0;
    //animation.timingFunction = UIViewAnimationCurveEaseInOut;
    //animation.type = @"pageCurl";
    ////animation.type = kCATransitionPush;
    //animation.subtype = kCATransitionFromRight;
    //[self.view.window.layer addAnimation:animation forKey:nil];
//    [self dismissViewControllerAnimated:YES completion:nil];
    
//    newsArticleViewModel = [[NewsArticleViewModel alloc]init];
//    newsArticleViewController = [[NewsArticleViewController alloc]initWithViewModel:newsArticleViewModel];
    NewsListTableViewModel * viewModel = [[NewsListTableViewModel alloc]init];
    NewsListTableViewController * vc = [[NewsListTableViewController alloc]initWithViewModel:viewModel];
    if (sender.tag == 10) {
        vc.title = @"健康常识";
    }
    else if (sender.tag == 11) {
        vc.title = @"处方用药";
    }
    else {
        vc.title = @"经验交流";
    }
    vc.hidesBottomBarWhenPushed = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewSetting
{
    self.view.backgroundColor = backgroundGrayColor;
    healthKnowledgeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [healthKnowledgeButton setTitle:@"健康常识" forState:UIControlStateNormal];
    [healthKnowledgeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    healthKnowledgeButton.tag = 10;
    healthKnowledgeButton.layer.masksToBounds = YES;
    healthKnowledgeButton.layer.cornerRadius = 40;
    healthKnowledgeButton.backgroundColor = [UIColor whiteColor];
    healthKnowledgeButton.layer.borderWidth = 3.0f;
    healthKnowledgeButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [healthKnowledgeButton addTarget:self action:@selector(switchToTestSelfView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:healthKnowledgeButton];
    
    prescriptionsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [prescriptionsButton setTitle:@"处方用药" forState:UIControlStateNormal];
    [prescriptionsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    prescriptionsButton.tag = 11;
    prescriptionsButton.layer.cornerRadius = 40;
    prescriptionsButton.layer.masksToBounds = YES;
    prescriptionsButton.layer.borderWidth = 3.0;
    prescriptionsButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    prescriptionsButton.backgroundColor = [UIColor whiteColor];
    [prescriptionsButton addTarget:self action:@selector(switchToTestSelfView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:prescriptionsButton];
    
    experienceExchangeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [experienceExchangeButton setTitle:@"经验交流" forState:UIControlStateNormal];
    [experienceExchangeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    experienceExchangeButton.tag = 12;
    experienceExchangeButton.layer.cornerRadius = 40;
    experienceExchangeButton.layer.masksToBounds = YES;
    experienceExchangeButton.layer.borderWidth = 3.0;
    experienceExchangeButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    experienceExchangeButton.backgroundColor = [UIColor whiteColor];
    [experienceExchangeButton addTarget:self action:@selector(switchToTestSelfView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:experienceExchangeButton];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_quit"] style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
}

- (void)logout
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"退出登录" message:@"是否确定退出登录" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert.rac_buttonClickedSignal subscribeNext:^(NSNumber * x) {
        if ([x integerValue] == 1) {
            [[[APIService shareService] logout] subscribeNext:^(id x) {
                [(AppDelegate *)[UIApplication sharedApplication].delegate showLoginView];
            }];
        }
    }];
    [alert show];
}

- (void)makeConstaints
{
    [super makeConstaints];
    WS(weakSelf);
    [healthKnowledgeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left).offset(20);
        make.top.equalTo(weakSelf.view.mas_top).offset(screenHeight/2-20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    
    [prescriptionsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.top.equalTo(weakSelf.view.mas_top).offset(screenHeight/2-20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    
    [experienceExchangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.view.mas_right).offset(-20);
        make.top.equalTo(weakSelf.view.mas_top).offset(screenHeight/2-20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
